package com.film.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.film.po.FilmType;
import com.film.service.FilmTypeService;

/**
 * 电影类型
 * 
 * @author Administrator
 *
 */
@RestController
public class FilmTypeController {

	// 电影类型的service
	private FilmTypeService filmTypeService;

	@Autowired
	public void setFilmTypeService(FilmTypeService filmTypeService) {
		this.filmTypeService = filmTypeService;
	}

	/**
	 * 添加
	 * @param filmType
	 * @return
	 */
	@RequestMapping(value = "/filmtype", method = RequestMethod.POST)
	public ResponseEntity<FilmType> add(@RequestBody FilmType filmType) {
		try {
			FilmType filmType2 = filmTypeService.add(filmType);
			return new ResponseEntity<FilmType>(filmType2, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<FilmType>(HttpStatus.CONFLICT);

	}

	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/filmtype/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<Void> deleteById(@PathVariable("id") int id) {
		try {
			filmTypeService.deleteById(id);
			return new ResponseEntity<Void>(HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);

	}

	/**
	 * 更新
	 * @param filmType
	 * @return
	 */
	@RequestMapping(value = "/filmtype", method = RequestMethod.PUT)
	public ResponseEntity<FilmType> update(@RequestBody FilmType filmType) {
		
		
		try {
			FilmType update = filmTypeService.update(filmType);
			return new ResponseEntity<FilmType>(update, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<FilmType>(HttpStatus.NOT_FOUND);
		
	}

	/**
	 * 根据id查
	 * @param id
	 * @return
	 * @throws InterruptedException 
	 */
	@RequestMapping(value = "/filmtype/{id}", method = RequestMethod.GET)
	public ResponseEntity<FilmType> findById(@PathVariable("id") int id) {
		
		try {
			FilmType filmType = filmTypeService.findById(id);
			return new ResponseEntity<FilmType>(filmType, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<FilmType>(HttpStatus.NOT_FOUND);

	}

	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping(value = "/filmtypes", method = RequestMethod.GET)
	public ResponseEntity<List<FilmType>> getList() {
		try {
			List<FilmType> list = filmTypeService.getList();
			return new ResponseEntity<List<FilmType>>(list, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<List<FilmType>>(HttpStatus.NO_CONTENT);

	}

}
